package com.example.okhttp;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements Callback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        OkhttpUtil.getContent("https://www.baidu.com/",this);

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                long startTime=System.currentTimeMillis();
//                HttpUrlConnUtil.getContent("http://www.qq.com");
//                long endTime = System.currentTimeMillis();
//                Log.e("aaaa","totaltime httpurl="+(endTime-startTime));
//            }
//        }).start();
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                long startTime = System.currentTimeMillis();
//                String str=OkhttpUtil.getContent("http://www.qq.com");
//                long endTime = System.currentTimeMillis();
//                Log.e("aaaa","totaltime okhttp="+(endTime-startTime)+str);
//            }
//        }).start();

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Map<String, String> map = new HashMap<String, String>();
//                map.put("key","96efc220a4196fafdfade0c9d1e897ac");
//                map.put("qq","892777597");
//                String content=OkhttpUtil.post("http://japi.juhe.cn/qqevaluate/qq",map);
//                Log.e("androidLog",content);
//            }
//        }).start();
    }


    public void getDatasync(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    long startTime= System.currentTimeMillis();
                    OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
                    Request request = new Request.Builder()
                            .url("http://www.qq.com")//请求接口。如果需要传参拼接到接口后面。
                            .build();//创建Request 对象
                    Response response = null;
                    response = client.newCall(request).execute();//得到Response 对象
                    long endTime = System.currentTimeMillis();
                    Log.e("aaaa","totaltime okhttp="+(endTime-startTime));
                    if (response.isSuccessful()) {
                        Log.d("kwwl","response.code()=="+response.code());
                        Log.d("kwwl","response.message()=="+response.message());
                        Log.d("kwwl","res=="+response.body().string());
                        //此时的代码执行在子线程，修改UI的操作请使用handler跳转到UI线程。
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onFailure(Call call, IOException e) {
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.e("androidLog",response.body().string());
    }
}
